package org.wattdepot.cli;

import org.wattdepot.client.WattDepotClient;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.resource.source.jaxb.Source;
import java.util.List;

/**
 * A simple command line interface to WattDepot that shows basic connection and processing.
 * 
 * @author Jarret Mizo, Shaun Ramento
 */
public class CommandLineInterface {
  /** The URL to the public WattDepot Server. */
  private String hostUri = "http://server.wattdepot.org:8182/wattdepot/";
  /** Holds an instance of the WattDepot client. */
  private WattDepotClient client;

  /**
   * Tries to connect to server and returns if there is a failure to connect to the server.
   */
  public CommandLineInterface() {
    client = new WattDepotClient(hostUri);
    // Stop right away if we cannot contact server.
    if (!client.isHealthy()) {
      System.out.println("Failed to connect to " + client.getWattDepotUri());
      return;
    }
  }

  /**
   * Gets the client information.
   * 
   * @return client information
   */
  public WattDepotClient getClient() {
    return this.client;
  }

  /**
   * Gets the source list.
   * 
   * @return source list
   * @throws WattDepotClientException if there is a problem with the source list
   */
  public List<Source> getSourceList() throws WattDepotClientException {
    return this.client.getSources();
  }

  /**
   * The main program that is invoked from 'java -jar wattdepot-cli.jar'.
   * 
   * @param args All args are ignored.
   * @throws WattDepotClientException If anything goes wrong.
   */
  public static void main(String[] args) throws WattDepotClientException {
    System.out.println("Starting CLI.");
    // Starts the user CLI
    UserCommandInterface user = new UserCommandInterface();
    user.startUserInterface();

    System.out.println("Exiting CLI.");
  }

}
